<?
/*
Library - Functions - Validation includes
UCCC
*/
define('STD_ALLOWED_TAGS','<br><b><i><a><s><hr><ul><ol><li>');
# Removes illicit characters from filenames
 function validateFilename($str){
   # Improvement: Allow only one point!
   return str_replace('...','',preg_replace("/([^a-zA-Z0-9_\.])/i", "", strtolower(trim($str))));
 }
# Checks validity of a Zip
 function validatePLZ($PLZ){
   if (preg_match('/^[0-9]{1,2,3,4,5}*$/', $PLZ)) return $PLZ;
   return false;
 }
# Adds http:// to a URL (if not allready there), and checks for validity
 function validateURL($URL) {
   $URL = trim($URL);
   if (strpos($URL,'http://') === false && strpos($URL,'www')==0) $URL = 'http://'.$URL;
   if (strpos($URL,'http://') !== false && substr_count($URL,'.') > 1 && strpos($URL,' ') === false) {
     return $URL;
   } else return false;
}
# Scans an email address on validity
 function validateEMail($mail) {
   if (preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}\$/i", $mail)) {
        if (function_exists("gethostbyname")) {
            $parts = explode("@", $mail);
			return gethostbyname($parts[1]) ? $mail:false;
		}
        return $mail;
   } else return false;
 }
# Strip all invalid characters from a string
 function validateText($text,$strict=false,$allowed_tags=STD_ALLOWED_TAGS) {
   $str = str_replace("\\","",trim(strip_tags($text,$allowed_tags)));
   if ($strict) {
     $str = htmlentities($str,ENT_NOQUOTES);
   } else {
     # 	The string through, and all "signs OUTSIDE of tags convert.
     $change = true; $x=-1;
     while ($x++<strlen($str)) {
       if ($str[$x] == '<') $change = false; else if ($str[$x]=='>') $change = true;
       if ($str[$x] == '"' && $change) $str = substr($str,0,$x).'&quot;'.substr($str,$x+1);
     }
   }
   return str_replace("\n",'<br>',str_replace("'",'&#039;',$str));
 }
?>